$(function () {
    $("[name='zjbcd_sbgs']").jgbmtree($("[name='zjbcd_sbbm']"), $("[name='zjbcd_sbr']"), $("#sbbmId").val(), '');

    $("#zjbcd_gys").qyjgzcombobox({
        onSuccess:function () {
            $("#zjbcd_gys").zcombobox("setMap", { id:$("#zjbcdgys_id").val(), jc:$("#zjbcdgys_jc").val()});
        }
    });

    $('#grid_zjbcd').datagrid({
        url:$.webPath() + "/jsgl/zjbcd/zjbcdList",
        queryParams:{
            wzbm:$('#zjbcd_wzbm').val(),
            wzmc:$("#zjbcd_wzmc").val(),
            gysId:$("#zjbcd_gys").val(),
            sbgsId:$("#toolbar_zjbcd input[name='zjbcd_sbgs']").val(),
            sbbmId:$("#sbbmId").val(),
            sbrId:$("#toolbar_zjbcd input[name='zjbcd_sbr']").val()
        },
        toolbar:"#toolbar_zjbcd",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        fit:true,
        rownumbers:true,
        showFooter:true,
        striped:true,
        columns:[
            [
                {field:'lb.z', title:'类别', align:'center', width:50},
                {field:'wzgg.code', title:'物资编码', align:'center', width:50},
                {field:'wzgg.name', title:'物资名称', align:'center', width:40},
                {field:'wzgg.ggxh', title:'规格型号', align:'center', width:40},
                {field:'wzgg.dw', title:'单位', align:'center', width:20},
                {field:'gys.qc', title:'供应商', align:'center', width:40},
                {field:'je', title:'金额(元)', align:'right', width:40, options:{precision:4, min:0, groupSeparator:','},
                    formatter:function (value) {
                        var sl = formatJeForHtml2(value, ",");
                        return sl;
                    }
                },
                {field:'cgd.cgdh', align:'center', title:'采购单号', width:50},
                {field:'zt', align:'center', title:'状态', width:30, formatter:formatZjbcdSp}
            ]
        ],
        loadFilter:function (data) {
            $.each(data.rows, function () {
                if (this.zt) {
                    this.zt = this.zt == 'ytj' ? "已提交" : this.zt == 'ysp' ? "已审批" : this.zt == 'ysy' ? "已使用" : "已保存";
                }
            })

            return data;
        },
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        rowStyler:function (index, row) {
            if (row.zt == '已使用') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        },
        onSelect:function (rowIndex, rowData) {
            if (rowData && rowData.zt && rowData.zt == '已使用') {
                $("#btn_zjbcd_use").hide();
                $("#btn_zjbcd_apply").hide();
            } else if (rowData && rowData.zt && rowData.zt == '已审批') {
                $("#btn_zjbcd_use").show();
                $("#btn_zjbcd_apply").hide();
            } else {
                $("#btn_zjbcd_use").hide();
                $("#btn_zjbcd_apply").show();
            }
        }
    });

    $("#btn_zjbcd_search").click(function () {
        $('#grid_zjbcd').datagrid('load',
            {
                wzbm:$('#zjbcd_wzbm').val(),
                wzmc:$("#zjbcd_wzmc").val(),
                gysId:$("#zjbcd_gys").val(),
                sbgsId:$("#toolbar_zjbcd input[name='zjbcd_sbgs']").val(),
                sbbmId:$("#toolbar_zjbcd input[name='zjbcd_sbbm']").val(),
                sbrId:$("#toolbar_zjbcd input[name='zjbcd_sbr']").val()
            }
        );
    });


    $("#btn_zjbcd_apply").click(function () {
        var record = $("#grid_zjbcd").datagrid("getSelected");
        if (record == null) {
            $.messager.alert("提示", "请选择要申请的资金补差单!", "info");
            return;
        }
        $.messager.confirm('确认', "是否确认申请此资金补差单", function (r) {
            if (r) {
                $.post($.webPath() + '/jsgl/zjbcd/commit_zjbcd', {zjbcdId:record.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200') {
                        $('#grid_zjbcd').datagrid('reload');
                        $.messager.show({title:'提示框', msg:obj.message, timeout:3000, showType:'slide'});
                    } else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    })

    $("#btn_zjbcd_use").click(function () {
        var row = $('#grid_zjbcd').datagrid('getSelected');
        var win = $.window({
            title:"选择卡号",
            iconCls:'icon-add2',
            width:1000,
            height:500,
            cache:false,
            url:$.webPath() + "/jsgl/zjbcd/kcsearch?gysId=" + row['gys.id'],
            modal:true,
            buttons:[
                {
                    text:'保存',
                    handler:function () {
                        var kcrow = $("#grid_zjbcd_kc").datagrid('getSelected');
                        $.post($.webPath() + "/jsgl/zjbcd/useZjbcd", {'zjbcdId':row.id, 'khId':kcrow.id}, function (data) {
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window("close");
                                $('#grid_zjbcd').datagrid('reload');
                            } else
                                $.messager.alert('提示', json.message, 'info');
                        });
                    }
                }
            ]
        });
    })
})

function formatZjbcdSp(val, row) {
    if (!row.processInstanceId) {
        return val;
    } else {
        return '<a href="javascript:viewZjbcdFlowPicture();" style="color: red">' + val + '</a>';
    }
}

function viewZjbcdFlowPicture() {
    var row = $('#grid_zjbcd').datagrid('getSelected');
    $.window({
        title:"流程历史",
        iconCls:'icon-add2',
        url:$.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)) + "&businessNo=" + row['jsd.jsdh'],
        width:650,
        height:500,
        closed:true,
        modal:true,
        isMax:true
    });
}